
import { App } from './app';
import { EventName } from '../Frame/Script/const/eventName';
import { ToolsHelper } from '../Frame/Script/toolsHelper';
import { NetInfo } from './core/NetInfo';
import { MsgJoinOrLeaveLivingRoom } from './network/net_proto/protocols/game/MsgJoinOrLeaveLivingRoom';
import { MsgJoinOrLeaveSeat } from './network/net_proto/protocols/game/MsgJoinOrLeaveSeat';
import { MsgReadyOrNot } from './network/net_proto/protocols/game/MsgReadyOrNot';
import { MsgSelectModel } from './network/net_proto/protocols/game/MsgSelectModel';
import { MsgStartGame } from './network/net_proto/protocols/game/MsgStartGame';
import { ViewName } from '../Frame/Script/const/viewNameConst';
const { ccclass, property } = cc._decorator;

@ccclass
export class Main extends cc.Component {

    /** 游戏启动 */
    protected onLoad(): void {
        this.initTestUser();
        App.event.on(EventName.Game.USER_INFO, this.updateSelfUserInfo, this);
        App.event.on(EventName.Game.NATIVE_LEAVE_ROOM, this.nativeLeaveRoom, this);
        App.event.on(EventName.Game.JOIN_OR_LEAVE_ROOM, this.onJoinOrLeaveRoom, this);
        App.event.on(EventName.Game.JOIN_OR_LEAVE_SEAT, this.onJoinOrLeaveSeat, this);
        App.event.on(EventName.Game.READY_OR_NOT, this.onReadyOrNot, this);
        App.event.on(EventName.Game.SElECT_MODEL, this.onSelectModel, this);
        App.event.on(EventName.Game.START_GAME, this.onStartGame, this);

        App.init(this.node);
    }

    start() {
        // 测试游戏界面动画使用
        // App.view.openView(ViewName.Single.eModel1View);
        // App.view.openView(ViewName.Single.eModel2View);
    }

    protected onDestroy(): void {
        App.event.offAll(this);
    }

    /** 离开房间：原生端调用 */
    private async nativeLeaveRoom(data: string) {
        console.log("native_leave_room", data);
        let msg = await App.ws.callApi("game/ExitLivingRoom", {
            roomId: App.user.roomId,
            session: NetInfo.session
        })
        console.log("ExitLivingRoom=>", msg);
        if (msg.isSucc) {
            // cc.game.end();
        }
    }

    /** 监听玩家加入和离开房间 */
    private onJoinOrLeaveRoom(data: MsgJoinOrLeaveLivingRoom) {
        console.log("MsgJoinOrLeaveLivingRoom=>", data);
        if (data.isJoin) {
            console.log("玩家加入房间: " + App.user.roomId);
        } else {
            let seats = App.gameData.getGameSeats();
            for (let i = 0; i < seats.length; i++) {
                let seat_data = seats[i];
                if (seat_data.user && seat_data.user.uid === data.user.uid) {
                    seats[i] = null;
                }
            }
        }
        App.event.emit(EventName.Game.JOIN_OR_LEAVE_ROOM_TO_VIEW, data);
    }

    /** 监听玩家加入和离开座位 */
    private onJoinOrLeaveSeat(data: MsgJoinOrLeaveSeat) {
        console.log("MsgJoinOrLeaveSeat=>", data);
        let seats = App.gameData.getGameSeats();
        if (data.isJoin) {
            if (data.uid === NetInfo.uid) {
                App.user.updateUserInfo({ seatId: data.seatId });
            }
            seats[data.seatId - 1].user = data.joinUser;
        } else {
            if (data.uid === NetInfo.uid) {
                App.user.updateUserInfo({ seatId: 0 });
            }
            seats[data.seatId - 1].user = null;
        }
        App.event.emit(EventName.Game.JOIN_OR_LEAVE_SEAT_TO_VIEW, data);
    }

    /** 监听玩家准备 */
    private onReadyOrNot(data: MsgReadyOrNot) {
        console.log("MsgReadyOrNot=>", data);
        let index = data.seatId - 1;
        let seats = App.gameData.getGameSeats();
        seats[index].user.isReady = data.isReady;
        App.event.emit(EventName.Game.READY_OR_NOT_TO_VIEW, data);
    }

    /** 监听模式切换 */
    private onSelectModel(data: MsgSelectModel) {
        console.log("MsgSelectModel=>", data);
        App.gameData.setModelData(data.model);
        App.gameData.setTypeData(data.subModel);
        App.event.emit(EventName.Game.SELECT_MODEL_TO_VIEW, data);
    }

    /** 监听游戏开始 */
    private onStartGame(data: MsgStartGame) {
        console.log("MsgStartGame=>", data);
        let num = 0;
        let seats = App.gameData.getGameSeats();
        seats.forEach(p => { if (p.user) num += 1 });
        App.gameData.setPeopleNum(num);
        App.event.emit(EventName.Game.START_GAME_TO_VIEW, data);
    }

    /** 更新玩家信息：监听原生端消息 */
    private updateSelfUserInfo(data: string) {
        // 1001|808|张三|789987
        let dataArr = data.split("|");
        let user_data = {
            id: dataArr[0],
            roomId: dataArr[1],
            name: dataArr[2],
            avatar_url: dataArr[3]
        }
        App.user.updateUserInfo(user_data);
        console.log("native_user_info", data);
        App.initHttp();
    }

    /** 仅在测试阶段使用 */
    private initTestUser() {
        let roomId = "276";
        let id = '' + ToolsHelper.getRandom(1000, 10000);
        let name = "ID";
        let avatar_url = "https://p9.itc.cn/q_70/images03/20211125/796235655f5f4a2aa4177071820a9925.jpeg";
        let user_data = {
            id: id,
            roomId: roomId,
            name: name + id,
            avatar_url: avatar_url
        }
        App.user.updateUserInfo(user_data);
        App.initHttp();
    }
}


